How to use Twitter4j With JSF2 for Login? [on hold]
Posted
by
subodh
on Programmers
See other posts from Programmers
or by subodh
Published on 2014-06-10T02:52:00Z
Indexed on
2014/06/10
3:41 UTC
Read the original article
Hit count: 161
I am trying to do login with Twitter and using Twitter4j for that and wrote this code
In JSF
<h:commandButton id="twitterbutton" value="Sign up with Twitter"
action="#{twitterLoginBean.redirectTwitterLogin}" immediate="true" styleClass="twitterbutton"/>
In ManagedBean
public String redirectTwitterLogin() throws ServletException, IOException,
TwitterException {
HttpServletRequest request = (HttpServletRequest) FacesContext
.getCurrentInstance().getExternalContext().getRequest();
HttpServletResponse response = (HttpServletResponse) FacesContext
.getCurrentInstance().getExternalContext().getResponse();
Twitter twitter = TwitterFactory.getSingleton();
twitter.setOAuthConsumer(apiKey, apiSecret);
RequestToken requestToken = twitter.getOAuthRequestToken();
if (requestToken != null) {
AccessToken accessToken = null;
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
while (null == accessToken) {
try {
String pin = br.readLine();
accessToken = twitter
.getOAuthAccessToken(requestToken, pin);
} catch (TwitterException te) {
System.out
.println("Failed to get access token, caused by: "
+ te.getMessage());
System.out.println("Retry input PIN");
}
}
request.setAttribute(IS_AUTHENTICATED, true);
if (accessToken != null) {
LOGGER.debug("We have a valid oauth token! Make the facebook request");
doApiCall(twitter, request, response);
return null;
}
} else {
LOGGER.debug("We don't have auth code yet, fetching the Authorization URL...");
String authorizationUrl = requestToken.getAuthorizationURL();
LOGGER.debug("Redirecting to the Authorization URL: {}",
authorizationUrl);
request.setAttribute(IS_AUTHENTICATED, false);
redirect(authorizationUrl, response);
return null;
}
return null;
}
In above code i want first Login window of twitter will show and then again same method will call and after user will login i can show user information userId,Handel,location etc.
Redirect
private void redirect(String url, HttpServletResponse response)
throws IOException {
String urlWithSessionID = response.encodeRedirectURL(url);
response.sendRedirect(urlWithSessionID);
}
But this code is not working Can anyone tell better Solution for this ?
© Programmers or respective owner